package com.zhuxiang.client;

import com.zhuxiang.model.Question;
import com.zhuxiang.model.Student;
import com.zhuxiang.model.User;
import com.zhuxiang.model.UserMessage;
import com.zhuxiang.server.ServerDao;


import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 编程实现客户端的主页面绘制和相应功能的实现
 */
public class ClientView {

    private Client client;

    Student stu = new Student();
    Question question = new Question();
    List<Student> stuList = new ArrayList<>();
    List<Question> queList = new ArrayList<>();
    String path = "student.txt";
    String path1 = "question.txt";
    String path2 = "import.txt";
    String path3 = "export.txt";
    String str = "";

    public ClientView(Client client){
        this.client = client;
    }

    /**
     * 自定义成员方法实现客户端主页面
     */
    public void clientMainPage() throws IOException, ClassNotFoundException {
        while (true){
            System.out.println("  \n\n\t\t   在线考试系统");
            System.out.println("-------------------------------------------");
            System.out.print("   [1] 学员系统");
            System.out.println("     [2] 管理员系统");
            System.out.println("   [0] 退出系统");
            System.out.println("-------------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getSc().nextInt();
            switch (choose) {
                case 1:
                    str = clientStudentLogin();
                    if (!str.equals("fail") && !str.equals("add")){
                        StudentMainPage(str);
                    }
                    client.clientInit();
                    break;
                case 2:
                    int i = clientManagerLogin();
                    if (1 == i){
                        AdminMainPage();
                    }
                    client.clientInit();
                    break;
                case 0:
                    System.out.println("正在退出系统...");
                    client.clientClose();
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    /**
     * 编程实现学员系统主页面
     * @param str
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void StudentMainPage(String str) throws IOException, ClassNotFoundException {
        stuList = new ServerDao(stuList).readStu();
        while (true){
            System.out.println("  \n\n\t\t  学员系统");
            System.out.println("-------------------------------------------");
            System.out.print("   [1] 修改密码");
            System.out.println("     [2] 参加考试");
            System.out.println("   [0] 返回");
            System.out.println("-------------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getSc().nextInt();
            switch (choose) {
                case 1:
                    new ClientStuDao(stuList).update();
                    break;
                case 2:
                    System.out.println("正在进入考试模块...");
                    ExamMainPage();
                    break;
                case 0:
                    System.out.println("返回...");
                    client.clientInit();
                    clientMainPage();
                    break;
                default:
                    System.out.println("输入错误，请重新选择！");
            }

        }
    }

    /**
     * 编程实现考试系统主页面
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void ExamMainPage() throws IOException, ClassNotFoundException {
        stuList = new ServerDao(stuList).readStu();
        while (true){
            System.out.println("  \n\n\t\t  考试系统");
            System.out.println("-------------------------------------------");
            System.out.print("   [1] 开始考试");
            System.out.println("     [2] 导出成绩");
            System.out.print("   [3] 查询成绩");
            System.out.println("     [0] 返回");
            System.out.println("-------------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getSc().nextInt();
            switch (choose){
                case 1:
                    startExam(str);
                    break;
                case 2:
                    export();
                    break;
                case 3:
                    query();
                    break;
                case 0:
                    StudentMainPage(str);
                    break;
                default:
                    System.out.println("输入错误，请重新选择！");
                    
            }
        }
    }

    /**
     * 编程实现查询考试成绩的方法
     */
    private void query() throws IOException, ClassNotFoundException {
        stuList = new ServerDao(stuList).readStu();
        System.out.println("请输入id");
        int id = ClientScanner.getSc().nextInt();
        for (Student s: stuList) {
            if (id == s.getId()){
                String msg = (s.getScore() == null) ? s.getUserName()+"暂无成绩":s.getUserName()+":"+s.getScore();
                System.out.println(msg);
                return;
            }
        }
        System.out.println("id输入有误");
    }

    /**
     * 编程实现导出成绩的方法
     * @throws IOException
     */
    private void export() throws IOException, ClassNotFoundException {
        stuList = new ServerDao(stuList).readStu();
        FileOutputStream fos = new FileOutputStream(path3);
        for (Student s: stuList) {
            String msg = (s.getScore() == null) ? s.getUserName()+"暂无成绩":s.getUserName()+":"+s.getScore();
            fos.write((msg+"\r\n").getBytes());
        }
        System.out.println("导出成功");
    }

    /**
     * 编程实现参加考试的方法
     * @param str
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void startExam(String str) throws IOException, ClassNotFoundException {
        stuList = new ServerDao(stuList).readStu();
        queList = new ServerDao(queList).readExam();
        int count = 0;
        if (queList.size() == 0){
            System.out.println("尚未添加考题");
            return;
        }
        for (Question q: queList) {
            System.out.println("题目"+q.getQuestion());
            System.out.println("请输入答案");
            String answer = ClientScanner.getSc().next();
            if (q.getAnswer().equals(answer)){
                count++;
            }
        }
        int score = 100*count / queList.size();
        for (Student s: stuList) {
            if (str.equals(s.getUserName())){
                s.setScore(""+score);
            }
        }
        System.out.println("当前考试得分："+score);
        new ServerDao(stuList).writeStu();
    }

    /**
     * 编程实现学员登录方法
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private String clientStudentLogin() throws IOException, ClassNotFoundException {
        stuList = new ServerDao(stuList).readStu();
        // 1.准备学员登录的相关数据
        System.out.println("请输入学员的账户信息：");
        String userName = ClientScanner.getSc().next();
        System.out.println("请输入学员的密码信息：");
        String password = ClientScanner.getSc().next();
        UserMessage tum = new UserMessage("userCheck", new User(userName, password));
        // 2.将UserMessage类型的对象通过对象输出流发送给服务器
        client.getOos().writeObject(tum);
        System.out.println("客户端发送学员账户信息成功！");
        // 3.接收服务器的处理结果并给出提示
        tum = (UserMessage) client.getOis().readObject();
        if ("success".equals(tum.getType())) {
            System.out.println("登录成功，欢迎使用！");
            return tum.getUser().getUserName();
        } else if ("fail".equals(tum.getType())){
            System.out.println("用户名或密码错误！");
            return "fail";
        }else {
            System.out.println("该用户不存在，系统已自动添加");
            stu = new Student(stuList.size()+1,tum.getUser().getUserName(),tum.getUser().getPassword());
            stuList.add(stu);
            new ServerDao(stuList).writeStu();
            return "add";
        }
    }

    /**
     * 自定义管理员系统主页面
     */
    private void AdminMainPage() throws IOException, ClassNotFoundException {
        while (true){
            System.out.println("  \n\n\t\t   管理员系统");
            System.out.println("-------------------------------------------");
            System.out.print("   [1] 学员管理模块");
            System.out.println("     [2] 考题管理模块");
            System.out.println("   [0] 返回");
            System.out.println("-------------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getSc().nextInt();
            switch (choose) {
                case 1:
                    studentManagerPage();
                    break;
                case 2:
                    System.out.println("正在进入考题管理模块...");
                    questionManagerPage();
                    break;
                case 0:
                    System.out.println("返回...");
                    client.clientInit();
                    clientMainPage();
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    /**
     * 编程实现考试管理模块的主页面
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void questionManagerPage() throws IOException, ClassNotFoundException {
        queList = new ServerDao(queList).readExam();
        while (true){
            System.out.println("  \n\n\t\t   考试管理模块");
            System.out.println("-------------------------------------------");
            System.out.println("1.增加考题");
            System.out.println("2.删除考题");
            System.out.println("3.修改考题");
            System.out.println("4.查询考题");
            System.out.println("5.导入考题");
            System.out.println("0.返回");
            System.out.println("-------------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getSc().nextInt();
            switch (choose){
                case 1:
                    new ClientQueDao(queList).addQuestion();
                    break;
                case 2:
                   new ClientQueDao(queList).deleteQuestion();
                    break;
                case 3:
                   new ClientQueDao(queList).updateQuestion();
                    break;
                case 4:
                   new ClientQueDao(queList).queryQuestion();
                    break;
                case 5:
                  new ClientQueDao(queList).importQuestion();
                    break;
                case 0:
                    AdminMainPage();
                    break;
                default:
                    System.out.println("输入有误");
            }
        }
    }

    /**
     * 自定义成员方法实现客户端学员管理模块的功能
     */
    private void studentManagerPage() throws IOException, ClassNotFoundException {
        stuList = new ServerDao(stuList).readStu();
        while (true){
            System.out.println("=======学员管理模块=========");
            System.out.println("1.增加学生信息");
            System.out.println("2.删除学生信息");
            System.out.println("3.修改学生信息");
            System.out.println("4.根据学号查找");
            System.out.println("5.查询所有信息");
            System.out.println("0.返回");
            System.out.println("==========================");
            int choose = ClientScanner.getSc().nextInt();
            switch (choose){
                case 1:
                    System.out.println("增加学生信息");
                    new ClientStuDao(stuList).add();
                    break;
                case 2:
                    System.out.println("删除学生信息");
                    new ClientStuDao(stuList).delete();
                    break;
                case 3:
                    System.out.println("修改学生信息");
                    new ClientStuDao(stuList).update();
                    break;
                case  4:
                    System.out.println("根据学号查找");
                    new ClientStuDao(stuList).findById();
                    break;
                case 5:
                    System.out.println("查询所有信息");
                    new ClientStuDao(stuList).findAll();
                    break;
                case 0:
                    AdminMainPage();
                    break;
                default:
                    System.out.println("输入有误");

            }
        }
    }

    /**
     * 自定义成员方法实现客户端管理员登录的功能
     */
    private int clientManagerLogin() throws IOException, ClassNotFoundException {
        // 1.准备管理员登录的相关数据
        System.out.println("请输入管理员的账户信息：");
        String userName = ClientScanner.getSc().next();
        System.out.println("请输入管理员的密码信息：");
        String password = ClientScanner.getSc().next();
        UserMessage tum = new UserMessage("managerCheck", new User(userName, password));
        // 2.将UserMessage类型的对象通过对象输出流发送给服务器
        client.getOos().writeObject(tum);
        System.out.println("客户端发送管理员账户信息成功！");
        // 3.接收服务器的处理结果并给出提示
        tum = (UserMessage) client.getOis().readObject();
        if ("success".equals(tum.getType())) {
            System.out.println("登录成功，欢迎使用！");
            return 1;
        } else {
            System.out.println("用户名或密码错误！");
            return -1;
        }
    }
}
